/*
 * Copyright 2010 Yaroslav Klymko aka t3hnar
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ua.t3hnar.io.serializer;

import junit.framework.TestCase;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Random;

/**
 * @author Yaroslav Klymko aka t3hnar
 */
public class Utils {
// ------------------------------ FIELDS ------------------------------

    public static int MAX = 20 * 1024 * 1024;

// -------------------------- STATIC METHODS --------------------------

    public static void assertEquals(final InputStream expected, final InputStream actual) throws IOException {
        TestCase.assertNotNull(expected);
        TestCase.assertNotNull(actual);

        int exp;
        int act;
        while ((act = actual.read()) != -1) {
            exp = expected.read();
            TestCase.assertEquals(act, exp);
        }
        TestCase.assertEquals(-1, expected.read());
    }

    public static void assertEquals(final byte[] expected, final byte[] actual) {
        TestCase.assertEquals(expected.length, actual.length);
        for (int i = 0; i < expected.length; i++) {
            final byte exp = expected[i];
            final byte act = actual[i];
            TestCase.assertEquals(exp, act);
        }
    }


    public static byte[] crateByteArray(final int length) {
        byte[] bytes = new byte[length];
        final Random random = new Random();
        random.nextBytes(bytes);
        return bytes;
    }

    public static void writeBytes(final OutputStream outputStream, final byte[] bytes) throws IOException {
        final DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        dataOutputStream.writeInt(bytes.length);
        dataOutputStream.write(bytes);
        dataOutputStream.flush();
    }
}
